<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Register PHP functions as XPath functions</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="domxpath.registernamespace.html">« DOMXPath::registerNamespace</a></li>
      <li style="float: right;"><a href="ref.dom.html">DOM 函数 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.domxpath.html">DOMXPath</a></li>
    <li>Register PHP functions as XPath functions</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="domxpath.registerphpfunctions" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DOMXPath::registerPhpFunctions</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">DOMXPath::registerPhpFunctions</span> &mdash; <span class="dc-title">Register PHP functions as XPath functions</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-domxpath.registerphpfunctions-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>DOMXPath::registerPhpFunctions</strong></span>(<span class="methodparam"><span class="type"><span class="type">string</span>|<span class="type">array</span>|<span class="type">null</span></span> <code class="parameter">$restrict</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><span class="type void">void</span></span></div>

  <p class="para rdfs-comment">
   This method enables the ability to use PHP functions within XPath expressions.
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-domxpath.registerphpfunctions-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">restrict</code></dt>

     <dd>

      <p class="para">
       Use this parameter to only allow certain functions to be called from XPath.
      </p>
      <p class="para">
       This parameter can be either a <span class="type">string</span> (a function name) or 
       an <span class="type">array</span> of function names.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-domxpath.registerphpfunctions-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   没有返回值。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-domxpath.registerphpfunctions-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   The following examples use <var class="filename">book.xml</var> which contains the following:
  </p>
  <p class="para">
   <div class="example" id="example-5429">
    <p><strong>示例 #1 book.xml</strong></p>
    <div class="example-contents">
<div class="xmlcode"><pre class="xmlcode">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;books&gt;
 &lt;book&gt;
  &lt;title&gt;PHP Basics&lt;/title&gt;
  &lt;author&gt;Jim Smith&lt;/author&gt;
  &lt;author&gt;Jane Smith&lt;/author&gt;
 &lt;/book&gt;
 &lt;book&gt;
  &lt;title&gt;PHP Secrets&lt;/title&gt;
  &lt;author&gt;Jenny Smythe&lt;/author&gt;
 &lt;/book&gt;
 &lt;book&gt;
  &lt;title&gt;XML basics&lt;/title&gt;
  &lt;author&gt;Joe Black&lt;/author&gt;
 &lt;/book&gt;
&lt;/books&gt;</pre>
</div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5430">
    <p><strong>示例 #2 <span class="methodname"><strong>DOMXPath::registerPHPFunctions()</strong></span> with <code class="literal">php:functionString</code></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$doc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">load</span><span style="color: #007700">(</span><span style="color: #DD0000">'book.xml'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$xpath&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DOMXPath</span><span style="color: #007700">(</span><span style="color: #0000BB">$doc</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Register&nbsp;the&nbsp;php:&nbsp;namespace&nbsp;(required)<br /></span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerNamespace</span><span style="color: #007700">(</span><span style="color: #DD0000">"php"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"http://php.net/xpath"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Register&nbsp;PHP&nbsp;functions&nbsp;(no&nbsp;restrictions)<br /></span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerPHPFunctions</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;Call&nbsp;substr&nbsp;function&nbsp;on&nbsp;the&nbsp;book&nbsp;title<br /></span><span style="color: #0000BB">$nodes&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">'//book[php:functionString("substr",&nbsp;title,&nbsp;0,&nbsp;3)&nbsp;=&nbsp;"PHP"]'</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Found&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$nodes</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">length</span><span style="color: #007700">}</span><span style="color: #DD0000">&nbsp;books&nbsp;starting&nbsp;with&nbsp;'PHP':\n"</span><span style="color: #007700">;<br />foreach&nbsp;(</span><span style="color: #0000BB">$nodes&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$node</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$title&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$node</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getElementsByTagName</span><span style="color: #007700">(</span><span style="color: #DD0000">"title"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">item</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">nodeValue</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$author&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$node</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getElementsByTagName</span><span style="color: #007700">(</span><span style="color: #DD0000">"author"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">item</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">nodeValue</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$title</span><span style="color: #DD0000">&nbsp;by&nbsp;</span><span style="color: #0000BB">$author</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Found 2 books starting with &#039;PHP&#039;:
PHP Basics by Jim Smith
PHP Secrets by Jenny Smythe
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5431">
    <p><strong>示例 #3 <span class="methodname"><strong>DOMXPath::registerPHPFunctions()</strong></span> with <code class="literal">php:function</code></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$doc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">load</span><span style="color: #007700">(</span><span style="color: #DD0000">'book.xml'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$xpath&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DOMXPath</span><span style="color: #007700">(</span><span style="color: #0000BB">$doc</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Register&nbsp;the&nbsp;php:&nbsp;namespace&nbsp;(required)<br /></span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerNamespace</span><span style="color: #007700">(</span><span style="color: #DD0000">"php"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"http://php.net/xpath"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Register&nbsp;PHP&nbsp;functions&nbsp;(has_multiple&nbsp;only)<br /></span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerPHPFunctions</span><span style="color: #007700">(</span><span style="color: #DD0000">"has_multiple"</span><span style="color: #007700">);<br />&nbsp;<br />function&nbsp;</span><span style="color: #0000BB">has_multiple</span><span style="color: #007700">(</span><span style="color: #0000BB">$nodes</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Return&nbsp;true&nbsp;if&nbsp;more&nbsp;than&nbsp;one&nbsp;author<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$nodes</span><span style="color: #007700">)&nbsp;&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />}<br /></span><span style="color: #FF8000">//&nbsp;Filter&nbsp;books&nbsp;with&nbsp;multiple&nbsp;authors<br /></span><span style="color: #0000BB">$books&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">'//book[php:function("has_multiple",&nbsp;author)]'</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Books&nbsp;with&nbsp;multiple&nbsp;authors:\n"</span><span style="color: #007700">;<br />foreach&nbsp;(</span><span style="color: #0000BB">$books&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$book</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$book</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getElementsByTagName</span><span style="color: #007700">(</span><span style="color: #DD0000">"title"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">item</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">nodeValue&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Books with multiple authors:
PHP Basics
</pre></div>
    </div>
   </div>
  </p>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-domxpath.registerphpfunctions-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="domxpath.registernamespace.html" class="methodname" rel="rdfs-seeAlso">DOMXPath::registerNamespace()</a> - Registers the namespace with the DOMXPath object</span></li>
    <li class="member"><span class="methodname"><a href="domxpath.query.html" class="methodname" rel="rdfs-seeAlso">DOMXPath::query()</a> - Evaluates the given XPath expression</span></li>
    <li class="member"><span class="methodname"><a href="domxpath.evaluate.html" class="methodname" rel="rdfs-seeAlso">DOMXPath::evaluate()</a> - Evaluates the given XPath expression and returns a typed result if possible</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>